package cn.mmko.infrastructure.persistent.repository;

import cn.mmko.domain.strategy.model.entity.StrategyAwardEntity;
import cn.mmko.domain.strategy.model.entity.StrategyEntity;
import cn.mmko.domain.strategy.model.entity.StrategyRuleEntity;
import cn.mmko.domain.strategy.repository.IStrategyRepository;
import cn.mmko.infrastructure.persistent.dao.IStrategyAwardDao;
import cn.mmko.infrastructure.persistent.dao.IStrategyDao;
import cn.mmko.infrastructure.persistent.dao.IStrategyRuleDao;
import cn.mmko.infrastructure.persistent.po.Strategy;
import cn.mmko.infrastructure.persistent.po.StrategyAward;
import cn.mmko.infrastructure.persistent.po.StrategyRule;
import cn.mmko.infrastructure.persistent.redis.IRedisService;
import cn.mmko.types.common.Constants;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Repository
public class StrategyRepository implements IStrategyRepository {
    @Resource
    private IStrategyAwardDao strategyAwardDao;
    @Resource
    private IStrategyDao strategyDao;
    @Resource
    private IStrategyRuleDao strategyRuleDao;
    @Resource
    private IRedisService redisService;
    @Override
    public List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId) {
        String key= Constants.RedisKey.STRATEGY_AWARD_KEY+strategyId;
        List<StrategyAwardEntity> strategyAwardEntities=redisService.getValue(key);
        if(null!=strategyAwardEntities&&!strategyAwardEntities.isEmpty()){
            return strategyAwardEntities;
        }
        /** 库读数据*/
        List<StrategyAward> strategyAwards=strategyAwardDao.queryStrategyAwardListbyStrategyId(strategyId);
        strategyAwardEntities=new ArrayList<>(strategyAwards.size());
        for(StrategyAward strategyAward:strategyAwards){
            StrategyAwardEntity strategyAwardEntity = StrategyAwardEntity.builder()
                        .strategyId(strategyAward.getStrategyId())
                        .awardId(strategyAward.getAwardId())
                        .awardCount(strategyAward.getAwardCount())
                        .awardCountSurplus(strategyAward.getAwardCountSurplus())
                        .awardRate(strategyAward.getAwardRate())
                        .build();
            strategyAwardEntities.add(strategyAwardEntity);
        }
        redisService.setValue(key,strategyAwardEntities);
       return strategyAwardEntities;

    }

    @Override
    public void storeStrategyAwardSearchTables(String key, Integer rateRange, Map<Integer, Integer> shuffleStrategyAwardSearchTables) {
        redisService.setValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY+key,rateRange);
        Map<Integer,Integer> cacheRateTable=redisService.getMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY+key);
        cacheRateTable.putAll(shuffleStrategyAwardSearchTables);
    }

    @Override
    public int getRateRange(Long strategyId) {
        return getRateRange(String.valueOf(strategyId));
    }

    @Override
    public int getRateRange(String key) {
        return redisService.getValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY+key);
    }

    @Override
    public Integer getStrategyAwardAssemble(String key, Integer rateKey) {
        return redisService.getFromMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY+key,rateKey);
    }

    @Override
    public StrategyEntity queryStrategyEntityByStrategyId(Long strategyId) {
        String key= Constants.RedisKey.STRATEGY_KEY+strategyId;
        StrategyEntity strategyEntity=redisService.getValue(key);
        if(null!=strategyEntity) return strategyEntity;
        Strategy strategy=strategyDao.queryStrategyEntityByStrategyId(strategyId);
        strategyEntity = StrategyEntity.builder()
                .strategyId(strategy.getStrategyId())
                .strategyDesc(strategy.getStrategyDesc())
                .ruleModels(strategy.getRuleModels())
                .build();
        redisService.setValue(key,strategyEntity);
        return strategyEntity;
    }

    @Override
    public StrategyRuleEntity queryStrategyRule(Long strategyId, String ruleModel) {
        StrategyRule strategyRuleReq = new StrategyRule();
        strategyRuleReq.setStrategyId(strategyId);
        strategyRuleReq.setRuleModel(ruleModel);
        StrategyRule strategyRuleRes = strategyRuleDao.queryStrategyRule(strategyRuleReq);
        return StrategyRuleEntity.builder()
                .strategyId(strategyRuleRes.getStrategyId())
                .awardId(strategyRuleRes.getAwardId())
                .ruleType(strategyRuleRes.getRuleType())
                .ruleModel(strategyRuleRes.getRuleModel())
                .ruleValue(strategyRuleRes.getRuleValue())
                .ruleDesc(strategyRuleRes.getRuleDesc())
                .build();

    }
}
